/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.supplier.adapter;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.BuyerFilter;
import com.epfresh.supplier.entity.CategoryNames;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class AgentFilterContentViewBinder
        extends ItemViewBinder<BuyerFilter.NamesBean.ChildBean, AgentFilterContentViewBinder.ViewHolder>  implements View.OnClickListener{

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);

            if (onRecyclerViewItemClickListener != null && o instanceof BuyerFilter.NamesBean.ChildBean){
                onRecyclerViewItemClickListener.onItemClick(v,position,o);
            }
        }
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_dialog_filter_content, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull BuyerFilter.NamesBean.ChildBean category) {
        holder.tv_dialog_filter_content.setText(category.getName());
        holder.tv_dialog_filter_content.setTag(R.id.item_key_position,-1);
        holder.tv_dialog_filter_content.setTag(R.id.item_key_status,category);
        holder.tv_dialog_filter_content.setOnClickListener(this);
        if(category.isSelectFlag()){//0.5 ccc red
            holder.tv_dialog_filter_content.setTextColor(Color.RED);
            GradientDrawable gd = (GradientDrawable)holder.tv_dialog_filter_content.getBackground();
            gd.setStroke(LocalDisplay.dp2px(1f),Color.RED);
        }else{
            holder.tv_dialog_filter_content.setTextColor(Color.BLACK);
            GradientDrawable gd = (GradientDrawable)holder.tv_dialog_filter_content.getBackground();
            gd.setStroke(LocalDisplay.dp2px(1f),Color.parseColor("#cccccc"));
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_dialog_filter_content;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_dialog_filter_content = itemView.findViewById(R.id.tv_dialog_filter_content);
        }
    }
}
